package com.example.task1.ui.AllService.traffic;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import com.example.task1.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class TrafficActivity extends AppCompatActivity {

    private FrameLayout fragment;
    private BottomNavigationView trafficBottom;
    private FragmentManager supportFragmentManager;
    private ActionBar supportActionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traffic);
        supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("智慧交管");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        supportFragmentManager = getSupportFragmentManager();
        supportFragmentManager.beginTransaction().add(R.id.fragment, new TrafficHomeFragment()).commit();
        trafficBottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.home:
                        supportFragmentManager.beginTransaction().replace(R.id.fragment, new TrafficHomeFragment()).commit();
                        break;
                    case R.id.order:
                        supportFragmentManager.beginTransaction().replace(R.id.fragment, new TrafficOrderFragment()).commit();
                        break;
                    case R.id.traffic:
                        supportFragmentManager.beginTransaction().replace(R.id.fragment, new TrafficFineFragment()).commit();
                        break;
                    case R.id.user:
                        supportFragmentManager.beginTransaction().replace(R.id.fragment, new TrafficUserFragment()).commit();
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        fragment = (FrameLayout) findViewById(R.id.fragment);
        trafficBottom = (BottomNavigationView) findViewById(R.id.traffic_bottom);
    }
}